//
//  HMStatusToolBar.swift
//  新浪微博
//
//  Created by 饶鹏伟 on 16/4/12.
//  Copyright © 2016年 饶鹏伟. All rights reserved.
//

import UIKit
//底部工具条的视图
class HMStatusToolBar: UIView {

    
    //声明三个按钮
    var repostBtn: UIButton!
    var commentBtn: UIButton!
    var zanBtn: UIButton!
   
    var statusViewModel: HMStatusViewModel?{
        
        didSet{
            
            //给三个按钮设置值
            repostBtn.setTitle(statusViewModel?.repostTitle, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentTitle, forState: .Normal)
            zanBtn.setTitle(statusViewModel?.zanTitle, forState: .Normal)
        }
        
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        //设置显示界面UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
       //设置按钮
        repostBtn=addChildrenBtn("转发", imageName: "timeline_icon_retweet")
        commentBtn=addChildrenBtn("评论", imageName: "timeline_icon_comment")
        zanBtn=addChildrenBtn("赞", imageName: "timeline_icon_like")
        
       //设置约束 三个按钮平分父视图
        //视图平铺
        //设置转发的约束
        repostBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.top.equalTo(self)
        
        }
        
        //设置评论的约束
        commentBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(repostBtn.snp_right)
            make.top.bottom.equalTo(self)
            make.width.equalTo(repostBtn.snp_width)
        }
        
        //设置赞的约束
        zanBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(commentBtn.snp_right)
            make.top.bottom.right.equalTo(self)
            make.width.equalTo(commentBtn.snp_width)
        }
        
        let sep1=sepView()
        
        let sep2=sepView()
        
        // 处理分割线 的宽度 以后就是1/scale
        
        let w = 1 / UIScreen.mainScreen().scale
        //高度的比例 黄金比例
        let multiplied = 0.4
        
        //设置两条线的约束
        sep1.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(repostBtn.snp_right)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            make.width.equalTo(w)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        sep2.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(commentBtn.snp_right)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            make.width.equalTo(w)
            make.centerY.equalTo(self.snp_centerY)
        }

        
    }
    
    
    //设置底部工具条的分割线
    private func sepView() -> UIView{
        
        let line=UIView()
        
        line.backgroundColor=UIColor.redColor()
        
       addSubview(line)
        
        return line
    }
    
    
    //定义一个方法 传入相应的参数 得到按钮
    private func addChildrenBtn(title: String, imageName: String) -> UIButton{
        
        let btn=UIButton()
        
        //设置btn的相应的属性
        btn.titleLabel?.font=UIFont.systemFontOfSize(12)
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        //自适应大小
        btn.sizeToFit()
        //添加到视图上
        addSubview(btn)
        
       return btn
    }
    
}
